var a,b,c;
require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
// if(!userInfo){
//     location.href="/loginIndex"
// }
var vm = new Vue({
    el:"#adDetails",
    data:{
        isNav:2,
        isNav1:2,
        isVideo:false,
        isVideo1:true,
        loadFlag:false,
        id:"",
        enterpriseAdvById:{},//视频信息
        allAlbumByUserId:{
            list:{}
        },//相册
        // allAlbumByUserId1:[],
        // allAlbumByUserId2:[],
        allProductByUserId:{
            list:{}
        },//产品
        allProductByUserId1:[],
        allProductByUserId2:[],
        createDate:"",
        uid:"",
        loginUid:"",
        pageNumber:"1",
        praiseState:"",//点赞状态
        praiseCount:"",//点赞人数
        collectionState:"",//收藏状态
        historyId:"",//上一个播放视频id
        vList:[],//初始化播放列表
        vLen:"",
        curr:"0",
        isContinuity:true,//是否连续播放
        totalTime:"",//当前视频时长
        playEnterpriseAdvById:{},
        release:{},
        num:"",
        isShare:false,
        isScreen:"",
        whereYouAt:""
    },
    watch:{
        // getCanvas:function(){}
        // curr:function(){
        //     if(this.curr<this.vLen){
        //         vm.totalTime=0;
        //     }
        // }
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.id=this.searchObject&&this.searchObject.id;
        // this.loginUid=this.searchObject&&this.searchObject.uid;
        if(this.searchObject&&this.searchObject.loginUid){
            this.loginUid=this.searchObject&&this.searchObject.loginUid;
        }else{
            if(!userInfo){
                location.href="/loginIndex"
            }else{
                this.loginUid=userInfo.loginUid;
            }
        }
        this.releaseConfig();
        //this.getEnterpriseAdvById();
        console.log(this.id);
        lazyload();
    },
    methods:{
        //后退
        getBack:function(){
            //location.href="/see/enterpriseAd";
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //通过用户id 视频id state点赞状态 1点赞0取消点赞
        getPraise:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                id:that.id
                // state:that.praiseState
            };
            if(that.praiseState==0){
                data.state=1;
            }else{
                data.state=0;
            }

            console.log(data);
            this.ajax({url:"/see/getPraise",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.praiseCount=b.json.data;
                    if(that.praiseState==0){
                        that.praiseState=1;
                    }else{
                        that.praiseState=0;
                    }
                    // console.log(that.praiseCount)
                }else{
                    vm.$refs.prompt.toast("点赞失败！")
                }
            });
        },
        //通过用户id 视频id state收藏状态 1收藏0取消收藏
        getCollection:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                id:that.id
                // state:that.collectionState
            };
            if(that.collectionState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            //console.log(data)
            this.ajax({url:"/see/getCollection",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(that.collectionState==0){
                        that.collectionState=1;
                    }else{
                        that.collectionState=0;
                    }
                    // vm.$refs.prompt.toast("收藏成功！")
                    //console.log(that.collectionState)
                }else{
                    vm.$refs.prompt.toast("收藏失败！");
                }
            });
        },
        //播放完成给用户的积分
        getEndPlayEnterpriseAdvById:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                id:that.vList[that.curr].id
            };
            this.ajax({url:"/see/endPlayEnterpriseAdvById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.playEnterpriseAdvById=b.json.data;
                    that.num=2;
                    if(that.playEnterpriseAdvById.integral>0){
                        that.getIsScreen(that.playEnterpriseAdvById.type);
                    }else{
                        //that.continuity();
                    }
                }else{
                    that.num=3;
                    vm.$refs.prompt.toast("领取失败！");
                }
            });
        },
        getIsScreen:function(type){
            this.isScreen=type;
            if(type){
                this.continuity();
            }
        },
        //连续播放
        continuity:function(){
            if(vm.isContinuity){
                vm.curr++;
                if(this.curr<this.vLen){
                    this.num="";
                    vm.getEnterpriseAdvById(vm.vList[vm.curr].id);
                }
            }else{
                return false;
            }
        },
        //获取播放视频信息
        getEnterpriseAdvById:function(id){
            var that=this;
            if(id) {
                player.dispose();
                $(".adDetails-video").append("<div id='my-video'></div>");
                that.id = id;
                clearInterval(a);
                that.curr=0;
                that.vList= [];
                console.log("========")
                console.log(that.curr)
                console.log(that.vList)
                console.log("========")
                // myPlayer.pause();
            }else if(that.enterpriseAdvById){
                player.dispose();
                $(".adDetails-video").append("<div id='my-video'></div>");
            }
            var data={
                id:that.id,
                loginUid:that.loginUid
            };
            // console.log(myPlayer.src)
            this.ajax({url:"/see/getEnterpriseAdvById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.enterpriseAdvById=b.json.data;
                    that.uid=that.enterpriseAdvById.uid;
                    that.praiseState=that.enterpriseAdvById.praiseState;
                    that.praiseCount=that.enterpriseAdvById.praiseCount;
                    that.collectionState=that.enterpriseAdvById.collectionState;
                    that.historyId=that.enterpriseAdvById.historyId;
                    that.createDate=that.enterpriseAdvById.createDate.split(" ")[0].split("-");
                    that.createDate=that.createDate[0]+"年"+that.createDate[1]+"月"+that.createDate[2]+"日";
                    that.vList.push({"imgSrc":that.enterpriseAdvById.imgSrc,"id":that.enterpriseAdvById.id});
                    that.enterpriseAdvById.recommends.forEach(function(item,index){
                        that.vList.push({"imgSrc":item.coverImgSrc,"id":item.id});
                    });
                    that.vLen=that.vList.length;
                    getPlayer(that.vList[that.curr].imgSrc);
                    // player.play();
                    if(that.enterpriseAdvById&&that.enterpriseAdvById.officiaVideo){
                        getPlayer1(that.enterpriseAdvById.officiaVideo);
                    }
                    if(that.enterpriseAdvById.historyIdState){
                        console.log("dddd")
                        that.num=4;
                    }else{
                        //vm.getCanvas();
                    }
                    that.getHistoryAdd();
                }else{
                    vm.$refs.prompt.toast(b.json.msg||"获取播放视频信息失败！");
                }
            });
        },
        //增加播放历史 返回奖励信息
        getHistoryAdd:function(){
            var that=this,data;
            data={
                loginUid:that.uid,
                advId:that.id
            };
            this.ajax({url:"/happyi/user/historyAdd",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){

                }else{
                    vm.$refs.prompt.toast("增加播放历史失败！");
                }
            });
        },
        //获取配置文件
        releaseConfig:function(){
            var that=this,data;
            data={
                loginUid:that.loginUid
            };
            console.log(data)
            this.ajax({url:"/release/releaseConfig",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.release=b.json.data;
                    console.log(that.release)
                    that.getEnterpriseAdvById();
                }else{
                    vm.$refs.prompt.toast("获取视频信息失败！")
                }
            });
        },
        //显示视频等图标
        getIsVideo:function(type){
            if(type==1){
                this.isVideo=true;
                b=setInterval(this.hideTip, 5000);
            }else if(type==2){
                this.isVideo1=true;
                c=setInterval(this.hideTip1, 5000);
            }
        },
        hideTip:function(){
            this.isVideo=false;
            clearInterval(b);
        },
        hideTip1:function(){
            this.isVideo1=false;
            clearInterval(c);
        },
        //视频暂停
        getVideoPause:function(type){
            if(type==1){
                this.isNav=2;
                clearInterval(a);
                player.pause();
            }else if(type==2){
                this.isNav1=2;
                player1.pause();
            }
        },
        //视频播放
        getVideoPlay:function(type){
            if(type==1){
                this.isVideo=!this.isVideo;
                this.isNav=1;
                if(!this.enterpriseAdvById.historyIdState&&this.enterpriseAdvById.lastCount>0){
                    this.getCanvas();
                }
                player.play();
            }else if(type==2){
                this.isVideo1=!this.isVideo1;
                this.isNav1=1;
                player1.play();
            }
        },
        //上一个视频
        getLastvideo:function(){
            this.isVideo=!this.isVideo;
            // vm.totalTime=0;
            this.num="";
            this.isNav=1;
            player.dispose();
            $(".adDetails-video").append("<div id='my-video'></div>");
            //
            this.getEnterpriseAdvById(this.historyId);
            console.log(this.curr)
        },
        //下一个视频
        getNextvideo:function(){
            this.isVideo=!this.isVideo;
            // vm.totalTime=0;
            this.num="";
            vm.curr++;
            this.isNav=1;
            player.dispose();
            $(".adDetails-video").append("<div id='my-video'></div>");
            // player.dispose();
            this.getEnterpriseAdvById(vm.vList[vm.curr].id);
        },
        getVideoData:function(){
            var that=this;
            console.log(that.vList[that.curr]&&that.vList[that.curr].imgSrc);
            if(that.vList[that.curr]&&that.vList[that.curr].imgSrc){
                if(that.curr>= that.vLen){
                    return false;
                }else{
                    getPlayer(that.vList[that.curr].imgSrc);
                    // myPlayer.src(that.vList[that.curr].imgSrc);
                    // myPlayer.load();
                    // myPlayer.play();
                }
            }

        },
        //是否连续播放
        getContinuity:function(){
            this.isContinuity=!this.isContinuity;
            if(this.isContinuity){
                vm.curr++;
                this.getVideoData();
            }
        },
        //水球
        getCanvas:function(){
            var that=this;
            a=setInterval(function(){
                var totalTime=player.getDuration();
                var whereYouAt = player.getCurrentTime();
                if(whereYouAt>that.whereYouAt+2){
                    player.seek(that.whereYouAt+1);
                    return false;
                }
                that.whereYouAt=whereYouAt;
                that.totalTime=Math.floor(totalTime)-Math.floor(whereYouAt);
                console.log(vm.totalTime);
                if(that.totalTime==1){
                    that.totalTime=0;
                    clearInterval(a);
                    vm.getEndPlayEnterpriseAdvById();
                }else if(totalTime>30){
                    if(that.totalTime<=30){
                        that.num=1;
                    }
                }else{
                    if(!isNaN(totalTime)){
                        that.totalTime=totalTime;
                        that.num=1;
                    }
                }
            },1000);
        },
       //跳转
        getNext:function(num,item){
            var path,that=this;
            console.log(item)
            if(num==1){   //个人商品详情
                path="/userGoodsInfo?productId="+item.id;
            }else if(num==2){
                path="/see/adDetails?id="+item;
            }else if(num==3){
                path="/see/enterpriseInfo?uid="+that.loginUid+"&enterpriseId="+this.enterpriseAdvById.enterpriseId;
            }
            location.href=path;
        },
        getShare:function(){
            this.isShare=!this.isShare;
            if(!this.isShare){
                player.play();
            }else{
                clearInterval(a);
                player.pause();
            }
        },
        getChoice:function(num){
            if(num==1){
                location.href="/friend/choiceList?number=3&postId="+this.id;
            }else{
                location.href="/friend/forward?number=3&postId="+this.id;
            }
        }
    }
});
var player,player1;
function getPlayer(imgSrc){
    player = new Aliplayer({
        id: "my-video",
        "width":"100%",
        "height":"100%",
        //播放方式四：使用STS方式播放
        "vid": imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        "accessKeyId": vm.release.accessKeyId,
        "securityToken": vm.release.securityToken,
        "accessKeySecret": vm.release.accessKeySecret,
        "autoplay": false,
        "isLive": false,
        "rePlay": false,
        "playsinline": true,
        "preload": true,
        "controlBarVisibility": "click",
        "showBarTime": 0,
        "useH5Prism": true,
        "x5_type":'h5', //通过 video 属性 “x5-video-player-type” 声明启用同层H5播放器，支持的值：h5 https://x5.tencent.com/tbs/guide/video.html
        "x5_fullscreen":false,//通过 video 属性 “x5-video-player-fullscreen” 声明视频播放时是否进入到 TBS 的全屏模式，支持的值：true
        // "x5_video_position":"top",
        // "x5_orientation":"portraint",
        "skinLayout": [
            {
                "name": "controlBar",
                "align": "blabs",
                "x": 0,
                "y": 0,
                "children": [
                    {
                        "name": "progress",
                        "align": "blabs",
                        "x": 45,
                        "y": 15
                    },
                    {
                        "name": "timeDisplay",
                        "align": "tl",
                        "x": 7,
                        "y": 9
                    }
                ]
            }
        ]
    },function(player){
        console.log('播放器创建好了。');
        $("#my-video video").attr("poster",vm.enterpriseAdvById.coverImgSrc);
    });
}
function getPlayer1(imgSrc){
    player1 = new Aliplayer({
        id: "my-video1",
        width:"100%",
        height:"100%",
        //播放方式四：使用STS方式播放
        vid : imgSrc,//"d388b80a857f4333a39177a5df8005db",//imgSrc,
        autoplay:false,
        accessKeyId: vm.release.accessKeyId,
        securityToken: vm.release.securityToken,
        accessKeySecret: vm.release.accessKeySecret
    },function(player1){
        console.log('播放器创建好了。')
    });
}
function lazyload(){
    $("img.lazy").lazyload();
}